package me.luger.project.fof.service;

import me.luger.core.commom.util.DateUtil;
import me.luger.core.commom.util.JsonUtil;
import me.luger.core.dao.nutz.service.BaseService;
import me.luger.project.fof.entity.AssetStrategyInfo;
import me.luger.project.fof.entity.Benchmark;
import me.luger.project.fof.entity.FofTask;
import me.luger.project.fof.entity.ext.TaskParams;
import org.apache.commons.collections.map.HashedMap;
import org.nutz.dao.Dao;
import me.luger.project.fof.entity.BenchmarkPrice;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.*;

@Repository
public class BenchmarkPriceService extends BaseService<BenchmarkPrice> {
    public BenchmarkPriceService(Dao dao) {
        super(dao);
    }

    @Autowired
    private BenchmarkService benchmarkService;

    @Autowired
    private AssetStrategyInfoService assetStrategyInfoService;

    @Cacheable(value = "long_time_cache")
    public List<BenchmarkPrice> getBenchMarkPriceList(String windCode, Date startDate, Date endDate) {
        Sql sql = Sqls.create("select date,eod_price from benchmark_price where index_code = @windCode " +
                "and date >= @startDate and date <= @endDate ");
        sql.setParam("windCode",windCode);
        sql.setParam("startDate",startDate);
        sql.setParam("endDate",endDate);
        List<BenchmarkPrice> priceList = this.list(sql);
        return priceList;
    }

    @Cacheable(value = "long_time_cache")
    public Map<String,Double> getFullBenchMarkPriceList(String windCode, Date startDate, Date endDate) {
        List<BenchmarkPrice> benchmarkPriceList = this.getBenchMarkPriceList(windCode, startDate, endDate);
        if(null == benchmarkPriceList || benchmarkPriceList.isEmpty()){
            return null;
        }
        TreeMap<String,Double> datePriceMap = new TreeMap<String, Double>();
        for(BenchmarkPrice benchmarkPrice : benchmarkPriceList){
            datePriceMap.put(DateUtil.format(benchmarkPrice.getDate(),DateUtil.DATE_YYYY_MM_DD),benchmarkPrice.getEodPrice());
        }
        Double startPrice = datePriceMap.firstEntry().getValue();

        while (startDate.before(endDate)){
            String curDateFmt = DateUtil.format(startDate,DateUtil.DATE_YYYY_MM_DD);
            Double tempPrice = datePriceMap.get(curDateFmt);
            if(null == tempPrice){
                datePriceMap.put(curDateFmt,startPrice);
            }
            startPrice = datePriceMap.get(curDateFmt);
            startDate = DateUtil.add(startDate,24 * 60 * 60);
        }

//        List<Double> priceList = new ArrayList<Double>();
//        for (Iterator<String> it = datePriceMap.keySet().iterator(); it.hasNext();) {
//            priceList.add(datePriceMap.get(it.next()));
//        }

        return datePriceMap;
    }


    @Cacheable(value = "short_time_cache")
    public List<Map> getBenchMarkPriceTrendChart(FofTask task){
        List<Map> benchMarkPriceTrendMapList = new ArrayList<Map>();

        String paramStr = task.getParams();
        TaskParams taskParams = JsonUtil.parse(paramStr,TaskParams.class);
        List<TaskParams.BenchmarkParamsBean> benchmarkParamsBeanList = taskParams.getBenchmarkParams();
        String modelId = taskParams.getModelId();

        String backYear = "";
        if("1".equals(modelId)){
            backYear = taskParams.getModelParams().get(1).getValue();
        }else if("2".equals(modelId)){
            backYear = taskParams.getModelParams().get(0).getValue();
        }else if("3".equals(modelId)){
            backYear = taskParams.getModelParams().get(0).getValue();
        }else if("4".equals(modelId)){
            backYear = "3";
        }

        Calendar startCal = DateUtil.nowAsCalendar();
        Date startDate = null;
        Date endDate = DateUtil.nowAsDate();
        if("1".equals(backYear)){
            startCal.add(Calendar.YEAR,-1);
        }else if("2".equals(backYear)){
            startCal.add(Calendar.YEAR,-3);
        }else if("3".equals(backYear)){
            startCal.add(Calendar.YEAR,-5);
        }

        startDate = startCal.getTime();

        Map benchMarkPriceTrendMap = null;
        for(TaskParams.BenchmarkParamsBean param : benchmarkParamsBeanList){
            Integer id = param.getId();
            Benchmark benchmark = benchmarkService.fetchById(id);
            if(null == benchmark){
                continue;
            }
            String windCode = benchmark.getWindCode();
            int assetStrategyId = benchmark.getAssetStrategyId();
            AssetStrategyInfo assetStrategyInfo = assetStrategyInfoService.fetchById(assetStrategyId);
            benchMarkPriceTrendMap = new HashedMap();
            benchMarkPriceTrendMap.put("name",benchmark.getName());
            benchMarkPriceTrendMap.put("assetStrategyName",assetStrategyInfo.getName());
            Map<String,Double> benchmarkPriceList = getFullBenchMarkPriceList(windCode,startDate,endDate);
            List<Map> datePriceMapList = new ArrayList<Map>();
            Map<String,Object> datePriceMap = null;
            Double startPrice = benchmarkPriceList.get(DateUtil.format(startDate,DateUtil.DATE_YYYY_MM_DD));
            for(Map.Entry<String,Double> entry : benchmarkPriceList.entrySet()){
                datePriceMap = new HashedMap();
                datePriceMap.put("date", entry.getKey());
                datePriceMap.put("price",entry.getValue()/startPrice);
                datePriceMapList.add(datePriceMap);
            }
            benchMarkPriceTrendMap.put("priceList",datePriceMapList);
            benchMarkPriceTrendMapList.add(benchMarkPriceTrendMap);
        }
        return benchMarkPriceTrendMapList;
    }

}
